//
//  WKWebViewVC.m
//  InterceptUrl
//
//  Created by WSXiong on 2017/10/11.
//  Copyright © 2017年 CMCC_CMSZ. All rights reserved.
//

#import "WKWebViewVC.h"
#import <WebKit/WebKit.h>

@interface WKWebViewVC ()<WKNavigationDelegate,WKUIDelegate>

@property (nonatomic, strong) WKWebView *webView;

@property (nonatomic,strong) UIView *navBGView;

@end

@implementation WKWebViewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    [self setupNavBar];
    
    [self initWebView];
}

- (void)setupNavBar{
    self.navBGView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 64)];
    self.navBGView.backgroundColor = [UIColor colorWithRed:230/256.0 green:230/256.0 blue:230/256.0 alpha:1];
    [self.view addSubview:self.navBGView];
    
    UIButton *returnBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    returnBtn.frame = CGRectMake(0, 20, 80, 44);
    [returnBtn setTitle:@"<<返回" forState:UIControlStateNormal];
    [returnBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    returnBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.navBGView addSubview:returnBtn];
    [returnBtn addTarget:self action:@selector(returnBtn:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)returnBtn:(UIButton *)btn{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)initWebView{
    WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
    configuration.userContentController = [[WKUserContentController alloc]init];
    
    WKPreferences *perferences = [WKPreferences new];
    perferences.javaScriptCanOpenWindowsAutomatically = YES;
    perferences.minimumFontSize = 30.0;
    configuration.preferences = perferences;
    
    self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 64, WIDTH, HEIGHT - 64) configuration:configuration];
    //http://wxpay.wxutil.com/mch/pay/h5.v2.php
    //https://testpay.chinamobilesz.com/payprod-h5/merchant/cashier.jsp
    dispatch_async(dispatch_get_main_queue(), ^{
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:_inputUrlStr] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
        [self.webView loadRequest:request];
    });
    //访问本地html文件
//    NSString *urlStr = [[NSBundle mainBundle] pathForResource:@"alipayios.html" ofType:nil];
//    NSURL *fileUrl = [NSURL fileURLWithPath:urlStr];
//    [self.webView loadFileURL:fileUrl allowingReadAccessToURL:fileUrl];
    
    
    
    self.webView.navigationDelegate = self;
    self.webView.UIDelegate = self;
    [self.view addSubview:self.webView];
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler{
    NSURL *url = navigationAction.request.URL;
    NSString *strUrl = url.absoluteString;
    if ([strUrl hasPrefix:@"weixin://"] || [strUrl hasPrefix:@"alipays://"] || [strUrl hasPrefix:@"alipay://"]) {
        UIApplication *application = [UIApplication sharedApplication];
        if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
            [application openURL:url options:@{} completionHandler:nil];
        }else{
            [application openURL:url];
        }
        decisionHandler(WKNavigationActionPolicyCancel);
        return;
    }
    decisionHandler(WKNavigationActionPolicyAllow);
    
}

- (void)dealloc{
    self.webView.navigationDelegate = nil;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
